﻿using Application.CustomerService;
using Application.DepartmentService;
using Domain.Entity;
using Domain.Entity.bs;
using Domain.Entity.zct;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WMSManage.Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]//需要鉴权
    public class DepartmentController : ControllerBase
    {
        private readonly IDepartmentService departmentService;
        public DepartmentController(IDepartmentService departmentService)
        {
            this.departmentService = departmentService;
        }

        /// <summary>
        /// 查询所有部门信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Department>> GetDepartments()
        {
            return await departmentService.GetDepartments();
        }

        /// <summary>
        /// 查询所有部门信息：有分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<FenYe<Department>> GetDepartmentFenYe(int CurrentPage, string? Department_code,string? Department_name,int? Department_status)
        {
            FenYe<Department> fenYe = new FenYe<Department>()
            {
                CurrentPage = CurrentPage,
            };
            FenYe < Department > a = await departmentService.GetDepartmentsFenYe(fenYe, Department_code, Department_name, Department_status);
            return a;
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="addDepartmentsDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddDepartment(AddDepartmentDto addDepartmentDto)
        {
            return await departmentService.AddDepartment(addDepartmentDto);
        }

        /// <summary>
        /// 使用ID查询部门信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Department> GetDepartmentID(int id)
        {
            return await departmentService.GetDepartmentsID(id);
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="updateDepartmentsDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateDepartment(UpdateDepartmentDto updateDepartmentsDto)
        {
            return await departmentService.UpdateDepartments(updateDepartmentsDto);
        }

        /// <summary>
        /// 删除部门（标记删除）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> DeleteDepartment(int id)
        {
            try
            {
                var result = await departmentService.DeleteDepartment(id);

                // 根据 Repository 返回的 int 结果，返回不同的 HTTP 响应
                return result switch
                {
                    1 => Ok(new { code = 1, message = "部门删除成功" }),
                    0 => BadRequest(new { code = 0, message = "删除失败：该部门已被其他单据引用，无法删除" }),
                    -1 => NotFound(new { code = -1, message = "部门不存在或已被删除" }),
                    _ => StatusCode(500, new { code = -2, message = "服务器内部错误：未知的删除结果" })
                };
            }
            catch (System.Exception ex)
            {
                // 记录异常日志 (如果配置了日志系统)
                // _logger.LogError(ex, "删除部门 {SupplierId} 时发生异常", id);
                return StatusCode(500, new { code = -2, message = $"服务器内部错误: {ex.Message}" });
            }
        }
    }
}
